import MyData from "./MyData";
import MyPage from "./MyPage";

class MyClientData {
    public constructor(){ 
    }
    private allData:Array<any>=new Array();
    private pageListData:Array<any>=new Array();
    private filterData:Array<any>=new Array();
    private isFilter:boolean=false;
    public myPage:MyPage=new MyPage();
    
    public pageData:Array<any>=new Array();
    public selectedKeys:Array<string>=new Array();
    public selectedKey:string="";



    public setData(data:any){
        this.allData=data; 
        this.isFilter=false;
        this.selectedKey="";
        this.selectedKeys.splice(0,this.selectedKeys.length);

        this.myPage.current=1;
        this.myPage.total=this.allData.length;
         
        this.setPageData();
    }
    public filter(filters:Array<string>,v:string){
      
        if(v==""){ 
           
            this.isFilter=false;
            this.selectedKey="";
            this.selectedKeys.splice(0,this.selectedKeys.length);
            this.myPage.current=1;
            this.myPage.total=this.allData.length;
            this.setPageData();
        }else{
            
            this.filterData.splice(0,this.filterData.length);
            this.allData.forEach( (el:any)=>{      
                for(let i=0;i<filters.length;i++)          {
                    let field=filters[i];
                    if(el[field]&&el[field].toString().indexOf(v)>=0){
                        this.filterData.push(el);
                        break;
                    }
                } 
            })
            this.isFilter=true;
            this.selectedKey="";
            this.selectedKeys.splice(0,this.selectedKeys.length);
            this.myPage.current=1;
            this.myPage.total=this.filterData.length;
            this.setPageData();

        }
    }
    public getAllData(){
        return this.allData;
    }
    public getCurrData(){
        return this.isFilter?this.filterData:this.allData;
    }
    private setPageData(){
        this.pageListData.splice(0,this.pageListData.length);
        let data=this.getCurrData();
        let ib=0;
        while(ib<data.length){
            let pageRow=new Array();
            for(let i=ib;i<ib+this.myPage.limit&&i<data.length;i++ ){
                pageRow.push(data[ i]); 
            }
            this.pageListData.push(pageRow);
            ib+=this.myPage.limit;
        }
        
        this.copyPageData();
        this.selectedKey=this.pageData.length>0?this.pageData[0].Id:"";
    }
    public getPageData(){
        this.copyPageData();
        let row=MyData.getById(this.pageData,this.selectedKey);
        if(!row){
            this.selectedKey=this.pageData.length>0?this.pageData[0].Id:"";
        }       
        return  this.pageData;
    }
    public select(id:string){
        let row=MyData.getById(this.pageData,id);
        if(row){
            this.selectedKey=id;
          
            return;
        }
        for(let i=0;i<this.pageListData.length;i++){
            let rows=this.pageListData[i];
           
            row=MyData.getById(rows,id);
            if(row){
                this.myPage.current=i+1;
                this.copyPageData();
                this.selectedKey=id;
                break;
            }
        }
    }
    private copyPageData(){
        this.pageData.splice(0,this.pageData.length);
        let rows=this.pageListData[this.myPage.current-1];
        rows.forEach((el:any)=>{
            this.pageData.push(el);
        }
        );
    }
    public getSelectData( ){
        if(!this.selectedKey){
            return null;
        }
        let m=MyData.getById(this.allData,this.selectedKey);
        return m;
    }
    public next():boolean{
        //rowNo
        if(!this.selectedKey){
            return false;
        } 
        let data=this.pageData ;

        for(let i=0;i<data.length;i++){
            let m=data[i];
            if(m.Id==this.selectedKey){
                if(i==data.length-1){
                    if(this.myPage.current<this.myPage.total/this.myPage.limit){
                        this.myPage.current++;
                        this.copyPageData();
                        if(this.pageData.length>0) {
                            this.selectedKey=this.pageData[0].Id;
                            return true;
                        }
                        else{
                            return false;
                        }
                    }
                }else{
                    this.selectedKey=data[i+1].Id;    
                    return true;                
                }
                break;
            }
        }
        return false;
    }
    public getById(id:string){
        return MyData.getById(this.allData,id);
    }
    public currRow(){
        if(this.selectedKey){
            return this.getById(this.selectedKey);
        }
        else{
            return null;
        }        
    }
}

export default   MyClientData 
